<?php

namespace App\Http\Forms;

use App\Modules\Shop\Shop;
use Kris\LaravelFormBuilder\Form;
use Silber\Bouncer\Database\Role;

class PersonnelForm extends Form
{
    public function buildForm()
    {
        $role = $this->getData('role') ?? [];
        $this
            ->add('username', 'text', [
                'wrapper' => ['class' => 'form-group', 'style' => 'clear: both; margin-top: 10px;'],
                'label' => "账号",
                'rules' => 'required|string',
            ])
            ->add('name', 'text', [
                'label' => "姓名",
                'rules' => 'required|string',
            ]);
        $this->add('is_merchant_staff', 'select', [
            'label' => "是否是商家职员",
            'rules' => 'required|string',
            'choices' => [0 => "否", 1 => "是"],
        ]);
        $this->add('shop_id', 'select', [
            'attr' => ['class' => "form-control select2", 'multiple' => "multiple", "id" => 'shop_id'],
            'label' => "选择商家",
            'rules' => "nullable|array",
            'choices' => $this->getData('shop'),
        ]);
       
//        $this->add('shop_id', 'select', [
//            'label' => "选择商家*",
//            'rules' => 'required|string',
//            'choices' => $allShop,
//        ]);
//            ->add('avatar', 'image', [
//                'label' => "头像",
//                'rules' => 'required|string',
//                "type" => "avatar"
//            ])
//            ->add('is_clerk', 'checkbox', [
//                'label' => "是否是店员",
//                'rules' => 'nullable|string',
//            ]);
        $this->add('address', 'static', [
            'tag' => 'div',
            'attr' => ['class' => ''],
            'label' => '选择权限组:'
        ]);
        Role::query()
            ->where('name', '!=', 'superadmin')
            ->get()
            ->each(function ($item) use ($role) {
                $this->add("role[{$item->name}]", 'checkbox', [
                    'wrapper' => ['class' => 'form-group role-group', 'data-value' => $item->title],
                    'label' => $item->title,
                    'checked' => in_array($item->name, $role),
                ]);
            });
        $this->add('submit', 'submit', [
            'attr' => ['class' => "form-control btn btn-success"],
            'label' => '保存',
        ]);
    }
}
